home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / MENUITEM.CLS < prev    next >
Text File  |  1997-06-14  |  5KB  |  182 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "CMenuItem"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Option Explicit
  11.  
  12. '$ Uses MENULIST.CLS
  13.  
  14. ' Don't call this class directly (access only through CMenuList)
  15.  
  16. Private hWnd As Long, hMenu As Long
  17. Private idID As Long, iPos As Long, fSys As Boolean
  18. Private rmenuChild As CMenuList
  19.  
  20. Sub Class_Initialize()
  21.     Set rmenuChild = Nothing
  22. End Sub
  23.  
  24. Function Create(iPosA As Long, hMenuA As Long, _
  25.                 rmenuA As CMenuList) As Boolean
  26.     ' Store properties
  27.     BugAssert IsMenu(hMenuA)
  28.     hMenu = hMenuA: iPos = iPosA
  29.     hWnd = rmenuA.WinHandle: fSys = rmenuA.SysMenu
  30.     idID = GetMenuItemID(hMenu, iPos)
  31.     
  32.     ' Create new menu list for any submenu
  33.     If idID = -1 Then
  34.         Dim menu As New CMenuList, f As Boolean
  35.         ' Must set parent before creating--yuck!
  36.         Set menu.Parent = rmenuA
  37.         f = menu.Create(GetSubMenu(hMenu, iPos))
  38.         BugAssert f     ' Should never fail
  39.         Set rmenuChild = menu
  40.     End If
  41.     Create = True
  42. End Function
  43.  
  44. ' Read-only properties to get state
  45. Property Get Separator() As Boolean
  46.     Checked = MF_SEPARATOR And GetMenuState(hMenu, iPos, MF_BYPOSITION)
  47. End Property
  48.  
  49. Property Get ID() As Boolean
  50.     ID = idID
  51. End Property
  52.  
  53. Property Get Popup() As Boolean
  54.     Popup = (idID = -1)
  55. End Property
  56.  
  57. Property Get WinHandle() As Long
  58.     WinHandle = hWnd
  59. End Property
  60.  
  61. Property Get Child() As CMenuList
  62.     Set Child = rmenuChild
  63. End Property
  64.  
  65. ' Convert text to recognizable name by stripping unnecessary parts
  66. Property Get Name() As String
  67.     Dim ch As String, sText As String
  68.     Dim s As String, i As Integer, c As Integer
  69.     sText = Text
  70.     i = 1: c = Len(sText)
  71.  
  72.     ' Skip any leading spaces and tabs
  73.     Do While i <= c
  74.         ch = Mid$(sText, i, 1)
  75.         If ch <> sSpace And ch <> sTab Then Exit Do
  76.         i = i + 1
  77.     Loop
  78.  
  79.     ' Strip any ampersand (&) and chop off anything after tab or ...
  80.     Do While i <= c
  81.         Select Case ch
  82.         Case "&"
  83.             ' Continue, skipping ampersand
  84.         Case sTab
  85.             ' Truncate at any tab after leading tab
  86.             Exit Do
  87.         Case "."
  88.             If Mid$(sText, i, 3) = "..." Then
  89.                 Exit Do
  90.             Else
  91.                 s = s & ch
  92.             End If
  93.         Case Else
  94.             ' Append normal letters
  95.             s = s & ch
  96.         End Select
  97.         ' Next letter
  98.         i = i + 1
  99.         ch = Mid$(sText, i, 1)
  100.     Loop
  101.     Name = s
  102. End Property
  103.  
  104. ' Read/write properties to get or set state
  105. Property Get Text() As String
  106.     Dim s As String, c As Integer
  107.     Const cMaxStr = 80
  108.     s = String$(cMaxStr, 0)
  109.     c = GetMenuString(hMenu, iPos, s, cMaxStr, MF_BYPOSITION)
  110.     Text = Left$(s, c)
  111. End Property
  112.  
  113. Property Let Text(sTextA As String)
  114.     Dim afState As Long
  115.     afState = GetMenuState(hMenu, iPos, MF_BYPOSITION)
  116.     afState = afState Or MF_BYPOSITION Or MF_STRING
  117.     Call ModifyMenu(hMenu, iPos, afState, idID, sTextA)
  118. End Property
  119.  
  120. Property Get Disabled() As Boolean
  121.     Disabled = MF_DISABLED And GetMenuState(hMenu, iPos, MF_BYPOSITION)
  122. End Property
  123.  
  124. ' Windows allows Disabled Ungrayed menus, but we don't
  125. Property Let Disabled(fDisabledA As Boolean)
  126.     Dim afState As Long
  127.     afState = GetMenuState(hMenu, iPos, MF_BYPOSITION)
  128.     If fDisabledA Then
  129.         afState = afState Or MF_DISABLED Or MF_GRAYED
  130.     Else
  131.         afState = afState And Not (MF_DISABLED Or MF_GRAYED)
  132.     End If
  133.     Call EnableMenuItem(hMenu, iPos, afState Or MF_BYPOSITION)
  134.     DrawMenuBar hWnd
  135. End Property
  136.  
  137. Property Get Grayed() As Boolean
  138.     Grayed = MF_GRAYED And GetMenuState(hMenu, iPos, MF_BYPOSITION)
  139. End Property
  140.  
  141. ' Windows allows Grayed Enabled menus, but we don't
  142. Property Let Grayed(fGrayedA As Boolean)
  143.     Disabled = fGrayedA
  144. End Property
  145.  
  146. Property Get Checked() As Boolean
  147.     Checked = MF_CHECKED And GetMenuState(hMenu, iPos, MF_BYPOSITION)
  148. End Property
  149.  
  150. Property Let Checked(fCheckedA As Boolean)
  151.     Dim afState As Long
  152.     afState = GetMenuState(hMenu, iPos, MF_BYPOSITION)
  153.     If fCheckedA Then
  154.         afState = afState Or MF_CHECKED
  155.     Else
  156.         afState = afState And Not (MF_CHECKED)
  157.     End If
  158.     Call CheckMenuItem(hMenu, idID, afState)
  159.     DrawMenuBar hWnd
  160. End Property
  161.  
  162. ' Methods
  163. Sub Hilite()
  164.     Call HiliteMenuItem(hWnd, hMenu, iPos, MF_BYPOSITION Or MF_HILITE)
  165. End Sub
  166.  
  167. Sub UnHilite()
  168.     Call HiliteMenuItem(hWnd, hMenu, iPos, MF_BYPOSITION Or MF_UNHILITE)
  169. End Sub
  170.  
  171. Sub Remove()
  172.     Call RemoveMenu(hMenu, iPos, MF_BYPOSITION)
  173. End Sub
  174.  
  175. ' Execute current item and return results
  176. Function Execute() As Boolean
  177.     Dim iMsg As Long
  178.     iMsg = IIf(fSys, WM_SYSCOMMAND, WM_COMMAND)
  179.     Execute = (SendMessage(hWnd, iMsg, ByVal idID, ByVal 0&) = 0)
  180. End Function
  181. '
  182.